package com.example.test06thread.callable;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @Author: fxy
 * @Date: 2022/05/27/14:56
 * @Description:
 */
public class SemaphoreTest {
    public static void main(String[] args) {
        //线程数量；停车位；限流
        Semaphore semaphore = new Semaphore(3);
        //6辆车来停车
        for (int i=1;i<=6;i++){
            new Thread(()->{
                try {
                    //acquire得到
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"停入车位");
                    //TimeUnit.SECONDS.sleep(2);
                    System.out.println(Thread.currentThread().getName()+"离开车位");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //release释放
                    semaphore.release();
                }
            },String.valueOf(i)).start();
        }
    }
}
